package com.mygdx.game.group;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.mygdx.game.MainGame;
import com.mygdx.game.group.basegroup.BaseGroup;
import com.mygdx.game.resource.Res;

public class GameOverGroup extends BaseGroup {

    private Image bgImage;
    private Image exit;//退出游戏按钮
    private Image reStart;//再来一局按钮
    private Label title;//标题
    private Label scoreLabel;//分数标签

    public GameOverGroup(MainGame mainGame) {
        super(mainGame);
        init();
    }

    public void init(){
        setSize(getMainGame().getWorldWidth()-40,getMainGame().getWorldHeight()/5);
        bgImage = new Image(getMainGame().getTextureAtlas().findRegion(Res.Atlas.GAME_BLANK));
        bgImage.setColor(0,0,0,0.5f);
        bgImage.setSize(getWidth(),getHeight());
        addActor(bgImage);

        Label.LabelStyle style = new Label.LabelStyle();
        style.font = getMainGame().getBitmapFont();
        title = new Label("游戏结束!",style);
        title.setFontScale(0.4f);
        title.setSize(title.getPrefWidth(),title.getPrefHeight());
        title.setPosition(getWidth()/2-title.getWidth()/2,getHeight()-title.getHeight());
        addActor(title);

        scoreLabel = new Label("0 分",style);
        scoreLabel.setFontScale(0.4f);
        scoreLabel.setSize(scoreLabel.getPrefWidth(),scoreLabel.getPrefHeight());
        scoreLabel.setPosition(getWidth()/2-scoreLabel.getWidth()/2,getHeight()/2-scoreLabel.getHeight()/2);
        addActor(scoreLabel);

        exit = new Image(getMainGame().getTextureAtlas().findRegion(Res.Atlas.BTN_BACK));
        exit.setPosition(getWidth()-exit.getWidth()-10,10);
        exit.addListener(new ClickListener(){
            @Override
            public void clicked (InputEvent event, float x, float y){
                Gdx.app.exit();
            }
        });
        addActor(exit);

        reStart = new Image(getMainGame().getTextureAtlas().findRegion(Res.Atlas.BTN_AGAIN));
        reStart.setPosition(10,10);
        reStart.addListener(new ClickListener(){
            public void clicked (InputEvent event, float x, float y){
                getMainGame().getGameScreen().getHomeStage().reStart();
                getMainGame().getGameScreen().setShowGameOverStage(false);
            }
        });
        addActor(reStart);
    }

    public void setScore(int score){
        if (scoreLabel!= null){
            scoreLabel.setText(score+" 分");
            scoreLabel.setSize(scoreLabel.getPrefWidth(),scoreLabel.getPrefHeight());
            scoreLabel.setPosition(getWidth()/2-scoreLabel.getWidth()/2,getHeight()/2-scoreLabel.getHeight()/2);
        }
    }
}
